//
//  About.m
//  X-Touch2
//
//  Created by shengchao yang on 12-4-20.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "About.h"
#import "Singleton.h"
#import "Imports1.h"

#define animateTime 0.5f   //动画切换时间间隔

@interface About(Private)
- (void) add1fingerTap;
- (void) add1fingerLeftSwipe;
- (void) add1fingerRightSwipe;
- (void) addPinch;

- (void) nextPage;
- (void) previousPage;
- (void) refreshData;
- (BOOL) checkIndexRange:(BOOL) nextOrPrevious;
@end

@implementation About
- (id)initWithNibName:(NSString *)nibNameOrNil withObj:(NSString *)obj{
    if ([obj.lowercaseString isEqualToString:@"about"]) {
        imageFolder = @"XRmenu/AboutImgs";
    }else {
        imageFolder = @"XRmenu/HotelInfoImgs";
    }
    self = [super initWithNibName:nibNameOrNil bundle:nil];
    if (self) {        
        [self add1fingerTap];
        [self add1fingerLeftSwipe];
        [self add1fingerRightSwipe];
        [self addPinch];
        
        [self refreshData];//刷新界面数据
        
        imageView = (UIImageView *)[self.view viewWithTag:TagOfUIImageView];
    }
    return self;
}
#pragma mark - 添加手势
//单机
- (void) add1fingerTap{
	UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
    recognizer.numberOfTouchesRequired = 1;//多少根手指
    recognizer.numberOfTapsRequired = 1;//点击几次
	[self.view addGestureRecognizer:recognizer];
	[recognizer release];	
}
//手指从右滑向左
- (void) add1fingerLeftSwipe{
	UISwipeGestureRecognizer *recognizer1;
	recognizer1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];
	recognizer1.direction = UISwipeGestureRecognizerDirectionLeft;
	[self.view addGestureRecognizer:recognizer1];
	[recognizer1 release];
}
//手指从左滑向右
- (void) add1fingerRightSwipe{
	UISwipeGestureRecognizer *recognizer1;
	recognizer1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
	recognizer1.direction = UISwipeGestureRecognizerDirectionRight;
	[self.view addGestureRecognizer:recognizer1];
	[recognizer1 release];
}
//用手指捏 返回
- (void) addPinch{                                                                                                   
	UIPinchGestureRecognizer *recognizer1;
	recognizer1 = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchForm:)];
	[self.view addGestureRecognizer:recognizer1];
	[recognizer1 release];	
}

#pragma mark - 左右翻页手势delegate
//单击
- (void) handleTapFrom:(UITapGestureRecognizer *)recognizer {
    if (AppSession.isAnimating) {
        MyNSLog(@"上一个动画还未完成...");
        return;
    }
    [AppSession playMusic:VoiceFile(@"clicked.caf")];
    NSString *SysMsg = [NSString stringWithFormat:@"SysMsg_MessageBoxBaseView_%@", self.nibName];
    postN(SysMsg);
}
//看后面(右)的内容
- (void) handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
    [self nextPage];
}
//看前面(左)的内容
- (void) handleSwipeRight:(UISwipeGestureRecognizer *)recognizer {
    [self previousPage];
}
//用手指捏 退出全屏模式
- (void) handlePinchForm:(UIPinchGestureRecognizer *)recognizer{
    if (recognizer.scale < 0.3 && recognizer.enabled) {//手指捏的距离大于70%才触发
        [AppSession playMusic:VoiceFile(@"trash.caf")];
        recognizer.enabled=NO;//首先关闭手势
        NSString *SysMsg = [NSString stringWithFormat:@"SysMsg_MessageBoxBaseView_%@", self.nibName];
        postN(SysMsg);
    }
}

#pragma mark - Private Function
- (void) nextPage{
    if ([self checkIndexRange:YES]) {
        [AppSession playMusic:VoiceFile(@"trash.caf")];
        [self refreshData];
        
        UIImageView *dishImg = (UIImageView *)[self.view viewWithTag:TagOfUIImageView];    
        CATransition *animation = [CATransition animation];
        animation.duration = animateTime;
        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.fillMode = kCAFillModeForwards;
        animation.type = kCATransitionPush;
        animation.subtype = kCATransitionFromRight; 
        [dishImg.layer addAnimation:animation forKey:@"animation"];
    }else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TranslateStr(@"提示")
                                                        message:TranslateStr(@"最后一张图片")
                                                       delegate:self 
                                              cancelButtonTitle:TranslateStr(@"确定")
                                              otherButtonTitles:nil];
        [alert show];     
        [alert release];
    }
    
}
- (void) previousPage{
    if ([self checkIndexRange:NO]) {
        [AppSession playMusic:VoiceFile(@"trash.caf")];
        [self refreshData];
        
        UIImageView *dishImg = (UIImageView *)[self.view viewWithTag:TagOfUIImageView];    
        CATransition *animation = [CATransition animation];
        animation.duration = animateTime;
        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.fillMode = kCAFillModeForwards;
        animation.type = kCATransitionPush;
        animation.subtype = kCATransitionFromLeft; 
        [dishImg.layer addAnimation:animation forKey:@"animation"];
    }else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TranslateStr(@"提示")
                                                        message:TranslateStr(@"第一张图片")
                                                       delegate:self 
                                              cancelButtonTitle:TranslateStr(@"确定")
                                              otherButtonTitles:nil];    
        [alert show];
        [alert release];
    }
    
}
- (void) refreshData{
    _currentObj = (OtherImage *)[AppSession.otherImages objectAtIndex:AppSession.otherImageIndex];
    UIImageView *dishImg = (UIImageView *)[self.view viewWithTag:TagOfUIImageView];
    [dishImg setImage:GetGeneralImg(imageFolder, _currentObj.imageName)];
}
//判断是否可以翻页
-(BOOL) checkIndexRange:(BOOL) nextOrPrevious{
    if (nextOrPrevious) {//下一页
        if (AppSession.otherImageIndex+1 < [AppSession.otherImages count]){
            AppSession.otherImageIndex++;
            return YES;
        }else {
            return NO;
        }
    }else {//上一页
        if(AppSession.otherImageIndex-1 >= 0) {
            AppSession.otherImageIndex--;
            return YES;
        }else {
            return NO;
        }
    }
    return YES;
}


- (void)viewDidLoad{ 
    [super viewDidLoad];
}
- (void)dealloc{
    [super dealloc];
}
- (void)viewDidUnload{
    [super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
	return YES;
}
@end
